<template>
	<view class="main">
		<view class="flex align between topSearch">
			<view style="width: 80%;">
				<u-search bgColor="#fff" :showAction="false" placeholder="输入视频名称搜索" v-model="merge_keyword"
					@change="change"></u-search>
			</view>
			<view class="addBtn flex align" @click="show=true">添加<uni-icons type="plus" size="20"
					color="#fff"></uni-icons></view>
		</view>
		<view class="list flex align between" v-for="(item,index) in list" :key="index">
			<view class="list-left">
				<view>{{item.merge_keyword}}</view>
				<view>类型：{{item.keyword_type}}
					<text style="margin-left: 20rpx;" v-if="item.group.name!=null">分组：{{item.group.name}}</text>
				</view>
			</view>
			<view>
				<uni-icons style="margin-right: 20rpx;" @click="toGroup(item.id)" type="compose" size="20"
					color="#FFE9E0"></uni-icons>
				<uni-icons @click="toDel(item.id)" type="trash" size="20" color="#FFE9E0"></uni-icons>
			</view>
		</view>
		<xw-empty :isShow="empty" text="暂无数据" textColor="#777777"></xw-empty>
		<uni-load-more :status="status" v-if="!empty" :content-text="loadText"></uni-load-more>
		<u-popup :show="show" :round="10" mode="bottom">
			<view class="btomShow">
				<view class="closeempty" @click="show = false">
					<uni-icons type="closeempty" size="30"></uni-icons>
				</view>
				<view class="showTit">添加</view>
				<view class="title">关键词</view>
				<input placeholder="请输入关键词，建议2-30个字" v-model="keyword" />
				<view class="subBtn" @click="toAdd">确定</view>
			</view>
		</u-popup>
		<u-popup :show="show2" :round="10" mode="center">
			<view class="btomShow" style="width: 530rpx;">
				<view class="closeempty" @click="show2 = false">
					<uni-icons type="closeempty" size="30"></uni-icons>
				</view>
				<view class="showTit">选择分组</view>
				<view class="title">分组</view>
				<input placeholder="请选择" v-model="group_id_name" disabled @click="show3 = true" />
				<view class="subBtn" @click="toChange">确定</view>
			</view>
		</u-popup>
		<u-picker :show="show3" :columns="groupList" keyName="name" @cancel="show3 = false"
			@confirm="confirmGroup"></u-picker>
	</view>
</template>

<script>
	import {
		mergeKeywordIndex,
		mergeKeywordAdd,
		mergeKeywordDel,
		mergeKeywordGroupIndex,
		mergeKeywordChangeGroup
	} from '@/api/apiData.js';
	export default {
		data() {
			return {
				keyword: '',
				show: false,
				show2: false,
				totalPage: 1,
				page: 0,
				offset: 20,
				list: [],
				scrollTop: 0,
				empty: false,
				loadText: {
					loadmore: '点击或上拉加载更多',
					loading: '努力加载中',
					nomore: '实在没有了'
				},
				status: 'loadmore',
				merge_keyword: '',
				groupList: [],
				show3: false,
				group_id_name:'',
				id_data:'',
				group_id:''
			};
		},
		onLoad() {
			// console.log(decodeURIComponent('%7B%22merge_keyword%22%3A%22%E4%BA%91%E5%AA%92%22%7D'))
			this.getlist();
			this.getGroup()
		},
		onPageScroll(e) {
			const that = this;
			that.scrollTop = e.scrollTop;
		},
		onReachBottom() {
			const that = this;
			that.getlist();
		},
		methods: {
			confirmGroup(e) {
				console.log(e)
				this.group_id =e.value[0].id
				this.group_id_name =e.value[0].name
				this.show3 = false
			},
			async getGroup() {
				const result = await mergeKeywordGroupIndex()
				this.groupList = [result.rows]
			},
			async toChange(){
				if(this.group_id==''){
					uni.showToast({
						title:'请选择分组',
						icon:'none'
					})
					return false
				}
				let obj = {
					id_data:this.id_data,
					group_id:this.group_id,
				}
				let that = this
				const result = await mergeKeywordChangeGroup(obj)
				uni.showToast({
					title:result.msg,
					icon:'none'
				})
				setTimeout(()=>{
					this.id_data = ''
					this.group_id = ''
					this.group_id_name = ''
					this.show2 = false
					this.page = 0
					this.list = []
					this.getlist()
				},500)
			},
			toGroup(id) {
				this.id_data = id
				this.show2 = true
			},
			// 删除
			toDel(id) {
				let that = this
				uni.showModal({
					title: "提示",
					content: "确定要删除吗？",
					showCancel: true,
					success: res => {
						if (res.confirm) {
							that.delCom(id)
						} else {

						}
					}

				})
			},
			async delCom(id) {
				let obj = {
					action: 'del',
					ids: id
				}
				const result = await mergeKeywordDel(obj)
				if (result.code == 1) {
					uni.showToast({
						title: '删除成功'
					})
					this.show = false
					this.page = 0
					this.list = []
					this.getlist()
				} else {
					uni.showToast({
						title: result.msg,
						icon: 'none'
					})
				}
			},
			async toAdd() {
				if (this.keyword == '') {
					uni.showToast({
						title: '请输入关键字',
						icon: 'none'
					})
					return false
				}
				let obj = {
					'row[merge_keyword]': this.keyword
				}
				const result = await mergeKeywordAdd(obj)
				if (result.code == 1) {
					uni.showToast({
						title: '添加成功'
					})
					this.show = false
					this.page = 0
					this.list = []
					this.getlist()
				} else {
					uni.showToast({
						title: result.msg,
						icon: 'none'
					})
				}
			},
			change() {
				this.page = 0
				this.list = []
				this.getlist()
			},
			async getlist() {
				const that = this;
				const {
					totalPage,
					page,
					offset,
					list,
					group_id,
					type,
					merge_keyword
				} = that;

				let obj = {
					menu_url: 'merge_keyword',
					limit: offset,
					offset: page,
					sort: 'id',
					order: 'desc',
					_: new Date().getTime()
					// filter:encodeURIComponent(JSON.stringify(filter)),
					// op:encodeURIComponent(JSON.stringify(filter))
				};
				if (merge_keyword != '') {
					obj.filter = JSON.stringify({
						"merge_keyword": merge_keyword.toString()
					})
					obj.op = JSON.stringify({
						"merge_keyword": 'LIKE'
					})
				}
				that.status = 'loading';
				if (totalPage > page) {
					let result = await mergeKeywordIndex(obj);
					that.page = page + 1;
					if (result.total == 0) {
						that.empty = true;
						that.status = 'nomore';
						return;
					}
					that.empty = false;
					setTimeout(() => {
						that.status = 'loadmore';
					}, 500);
					that.list = list.concat(result.rows);
					that.totalPage = Math.ceil(result.total / offset);
				} else {
					setTimeout(() => {
						that.status = 'nomore';
					}, 500);
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.main {
		padding: 13rpx 30rpx;

		.topSearch {
			margin-bottom: 30rpx;

			.addBtn {
				background: #FFCDB8;
				color: white;
				border-radius: 14rpx;
				font-size: 20rpx;
				padding: 8rpx 14rpx;
			}
		}

		.list {
			background: #2D2D2D;
			padding: 19rpx 67rpx;
			margin-bottom: 20rpx;
			border-radius: 10rpx;

			.list-left {
				color: #FFE9E0;
				font-size: 20rpx;
				width: 80%;

				view:nth-child(1) {
					font-weight: bold;
					font-size: 32rpx;
					margin-bottom: 15rpx;
				}
			}
		}

		.btomShow {
			padding: 0 28rpx 150rpx;
			position: relative;

			.showTit {
				text-align: center;
				font-size: 32rpx;
				font-weight: bold;
				padding: 33rpx 0;
			}

			input {
				font-size: 20rpx;
				padding: 16rpx 24rpx;
				border-radius: 10rpx;
				border: solid 1rpx #707070;
			}

			.closeempty {
				position: absolute;
				top: 20rpx;
				right: 30rpx;

			}

			.title {
				font-size: 28rpx;
				margin: 25rpx 0;
			}

			.subBtn {
				font-size: 30rpx;
				color: white;
				padding: 8rpx 95rpx;
				background: #5D9CEB;
				width: fit-content;
				margin: 137rpx auto 0rpx;
				border-radius: 10rpx;
			}
		}
	}
</style>